<cfcomponent output="false">

	<cffunction name="saveMoves" access="public">
		<cfargument name="stateID" required="true" />
		<cfargument name="legalMoves" required="true" />
		<cfset var local = {} />
		
		<cfset var i = 0 />
			<cfloop from="1" to="#ArrayLen(legalMoves)#" index="i">
				<cfset local.newStateID = getStateByBinary(
						legalMoves[i].getState().serializeState(),
						legalMoves[i].getState().isCheckmate(),
						legalMoves[i].getState().isStalemate(),
						legalMoves[i].getState().getTurn()
						).ID
						>
				
				<cfquery name="local.updateState" datasource="tmeyer">
					Insert Into Move
					(Move,Old_State,New_state)
					VALUES
					(
						<cfqueryparam cfsqltype="cf_sql_varchar" value="#legalMoves[i].getMove()#">,
						<cfqueryparam cfsqltype="cf_sql_bigint" value="#arguments.stateID#">,
						<cfqueryparam cfsqltype="cf_sql_bigint" value="#local.newStateID#">
					)
				</cfquery>
			</cfloop>
			
			<cfquery name="local.updateState" datasource="tmeyer">
				UPDATE State set Moves_Solved = 1 WHERE ID = <cfqueryparam cfsqltype="cf_sql_bigint" value="#arguments.stateID#">
			</cfquery>
	</cffunction>
	
	<!------>
	
	<cffunction name="getStateByID" access="public">
		<cfargument name="stateID" required="true" />
		
		<cfset var local = {} />
		
		<cfquery name="local.getState" datasource="tmeyer">
			SELECT * FROM State where ID = <cfqueryparam cfsqltype="cf_sql_bigint" value="#arguments.stateID#">
		</cfquery>
		
		<cfreturn local.getState />
		
	</cffunction>
	
	<!------>
	
	<cffunction name="getMovesByStateID" access="public">
		<cfargument name="stateID" required="true" />

		<cfset var local = {} />
		
		<cfquery name="local.getMovesByStateID" datasource="tmeyer">
			SELECT * FROM Move where old_state = <cfqueryparam cfsqltype="cf_sql_bigint" value="#arguments.stateID#">
		</cfquery>
		
		<cfreturn local.getMovesByStateID />
		
	</cffunction>
	
	<!------>
	
	<cffunction name="getStateByBinary" access="public">
		<cfargument name="binaryString" required="true" />
		<cfargument name="isCheckmate" required="true" />
		<cfargument name="isStalemate" required="true" />
		<cfargument name="color" required="true" />						
		
		<cfset var local = {} />
		<cfset local.new = false />		
		<cftransaction>
			<cfquery name="local.getState" datasource="tmeyer">
				SELECT * FROM State WITH (TABLOCKX) where state = <cfqueryparam cfsqltype="cf_sql_blob" value="#arguments.binaryString#">
			</cfquery>
			
			<cfif local.getState.recordCount eq 0>
				<cfquery name="local.saveState" datasource="tmeyer">
					INSERT INTO State
					(state, moves_solved, is_checkmate, is_stalemate,color)
					VALUES
					(<cfqueryparam cfsqltype="cf_sql_blob" value="#arguments.binaryString#">,
					0,
					<cfqueryparam cfsqltype="cf_sql_bit" value="#arguments.isCheckmate#">,
					<cfqueryparam cfsqltype="cf_sql_bit" value="#arguments.isStalemate#">,			
					<cfqueryparam cfsqltype="cf_sql_char" value="#arguments.color#">
					)
					SELECT SCOPE_IDENTITY() as ID
				</cfquery>
				
				<cfset local.new = true />
			<cfelse>
				<cfreturn local.getState />
			</cfif>
		</cftransaction>
		
		<cfif local.new>
			<cfset addMessage(local.saveState.ID) />
			<cfreturn getStateByID(local.saveState.ID) />
		</cfif>
		
	</cffunction>
	
	<!------>
	
	<cffunction name="getUnsolvedStates" access="public">
	
		<cfset var local = {} />
	
		<cfquery name="local.getUnsolvedStates" datasource="tmeyer">
			SELECT TOP 100 * FROM State where moves_solved = 0 ORDER BY ID
		</cfquery>
		
		<cfreturn local.getUnsolvedStates>
		
	</cffunction>
	
	<!------>
	
	<cffunction name="addMessage" access="public">
		<cfargument name="stateID" required="true" />
		
		<cfset max_length = 100 />
		
		<cfif cgi.server_name eq "none">
			<cfif NOT StructKeyExists(request,"messages")>
				<cfset request.messages = ArrayNew(1) />
			</cfif>
			
			<cfif ArrayLen(request.messages) gte max_length>
				<cfset SendMessages() />
				<cfset request.messages = ArrayNew(1) />
			</cfif>
			
			<cfset ArrayAppend(request.messages,arguments.stateID)>
		</cfif>
		
	</cffunction>
	
	<!------>
	
	<cffunction name="sendMessages" access="public">
		<cfset var local = {} />
		
		<cfif StructKeyExists(request,"messages") and arraylen(request.messages) gt 0>
			<cfset local.message = {}  />
			<cfset local.args = {} />
			
			<cfset local.message.stateIDs = request.messages />
			<cfset local.args.status = 'SEND'>
			<cfset local.args.queue = 'dynamicQueues/Chess'>
			<cfset local.args.message = local.message>
			
			<!---<cfset local.trySend = false />
			<cfloop condition="local.trySend eq false">--->
				<cfset local.trySend = SendGatewayMessage('Chess',local.args) />
			<!---></cfloop>--->
			
		</cfif>
		
		
	</cffunction>
	
	<!------>

</cfcomponent>